//
//  RadioViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/15.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "RadioViewController.h"
#import "RESideMenu.h"
#import "MainDataHelper.h"
#import "RadioModel.h"
#import "UIImageView+WebCache.h"
#import "RadioTableViewCell.h"

@interface RadioViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *radioTableView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *headerImageArray;

@property (nonatomic, strong) NSMutableArray *hotArray;


// 定时器
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int page;

@end

@implementation RadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadRadioTableView];
    [self loadData];
    
    [self loadScrollView];
    
}

- (void)loadRadioTableView
{
    _radioTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _radioTableView.delegate = self;
    _radioTableView.dataSource = self;
    
    [self.view addSubview:_radioTableView];
    
    self.navigationController.navigationBar.translucent = NO;
    
    UINib *radioNib = [UINib nibWithNibName:@"RadioTableViewCell" bundle:nil];
    [_radioTableView registerNib:radioNib forCellReuseIdentifier:@"cell"];
}

#pragma mark - 获取数据
- (void)loadData
{
    self.dataArray = [NSMutableArray array];
    NSString *string = @"http://api2.pianke.me/ting/radio";
    NSString *bodyString = @"auth=W8Um3Eiixm3qtDC8h8GcDrlDJxQMeoN76M8GxpD2Qm3cWP22NFFQx1a7Ps&client=1&deviceid=70D9EF20-AB8B-42FD-937C-7D884DB58895&version=3.0.6";
    [MainDataHelper postDataWithUrlString:string andBodyString:bodyString compare:^(id object) {
        NSDictionary *dic = object;
        
        NSArray *array = dic[@"data"][@"alllist"];
        
        for (NSDictionary *dic1 in array) {
            RadioModel *model = [[RadioModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        
        
        [self.radioTableView reloadData];
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    RadioModel *model = _dataArray[indexPath.row];
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
    cell.titleLabel.text = model.title;
    
    NSDictionary *name = [NSDictionary dictionaryWithDictionary:model.userinfo];
    cell.unameLabel.text = name[@"uname"];
    cell.descLabel.text = model.desc;
    return cell;
}

#pragma mark - 轮播图
- (void)loadScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    
    _scrollView.bounces = NO;
    
    self.radioTableView.tableHeaderView = _scrollView;
    
    
    self.headerImageArray = [NSMutableArray array];
    NSString *string = @"http://api2.pianke.me/ting/radio";
    NSString *bodyString = @"auth=W8Um3Eiixm3qtDC8h8GcDrlDJxQMeoN76M8GxpD2Qm3cWP22NFFQx1a7Ps&client=1&deviceid=70D20EF9-AB8B-42FD-937C-7D884DB58895&version=3.0.6";
    [MainDataHelper postDataWithUrlString:string andBodyString:bodyString compare:^(id object) {
        NSDictionary *dic = object;
        //        NSLog(@"dic====%@",dic);
        NSArray *imageArray = dic[@"data"][@"carousel"];
        //        NSLog(@"dict2===%@",imageArray);
        
        for (NSDictionary *dic2 in imageArray) {
            RadioModel *model = [[RadioModel alloc] init];
            [model setValuesForKeysWithDictionary:dic2];
            [self.headerImageArray addObject:model];
        }
        
        for (int i = 0; i < _headerImageArray.count; i++) {
            RadioModel *model = _headerImageArray[i];
            
            UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _headerImageArray.count * self.view.frame.size.width, 120)];
            UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
            [headerImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil];
            [headView addSubview:headerImageView];
            [_scrollView addSubview:headView];
            
                    
        }
        
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * _headerImageArray.count, 0);
        
    }];
}



// 定时器放在viewwill里面
- (void)viewWillAppear:(BOOL)animated {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
    // 添加到主线程中去, 返回当前的消息循环对象
    [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)timer:(NSTimer *)sender {
    self.page++;
    // 这里的self.page < 一个数字 的这个你可以改数字,改了看看,juice知道是干什么的了
    if (self.page < 3) {
        [_scrollView setContentOffset:CGPointMake(self.page * self.view.frame.size.width, 0) animated:YES];
        
    }else {
        self.page = 0;
        [_scrollView setContentOffset:CGPointMake(self.page * self.view.frame.size.width, 0) animated:YES];
    }
}

//释放NSTimer
-(void)viewWillDisappear:(BOOL)animated
{
    [self.timer invalidate];
    self.timer = nil;
}




- (IBAction)backBarButtonItem:(UIBarButtonItem *)sender {
    [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
